<!--
 * @Author: xiaodong 565071984@qq.com
 * @Date: 2022-05-08 19:52:05
 * @LastEditors: xiaodong 565071984@qq.com
 * @LastEditTime: 2022-05-14 19:30:08
 * @FilePath: /小票/src/views/accountManagement/userManagement/components/UserManagementEdit.vue
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
-->
<template>
  <el-dialog
    :title="title"
    :visible.sync="dialogFormVisible"
    width="500px"
    @close="close"
  >
    <el-form ref="form" :model="form" :rules="rules" label-width="100px">
      <el-form-item
        v-if="form && form.user_id"
        :label="$t('accountManage.agentAccountNumber') /*代理商账号*/"
        prop="username"
      >
        <el-input
          v-model.trim="form.username"
          autocomplete="off"
          disabled
        ></el-input>
      </el-form-item>
      <el-form-item v-else label="选择代理商账号">
        <el-select v-model="form.username" placeholder="请选账号">
          <el-option
            v-for="(item, index) in canAddAgentList"
            :key="index"
            :label="item.name"
            :value="item.name"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item
        :label="$t('common.username') /*用户名*/"
        prop="realname"
        :required="false"
      >
        <el-input v-model.trim="form.realname" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item :label="$t('common.iphone') /*联系电话*/" prop="phone">
        <el-input v-model.trim="form.phone" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item
        :label="$t('common.email') /*email*/"
        prop="email"
        :required="false"
      >
        <el-input v-model.trim="form.email" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item :label="$t('accountManage.remark') /*备注*/" prop="mark">
        <el-input
          v-model.trim="form.mark"
          autocomplete="off"
          type="textarea"
        ></el-input>
      </el-form-item>
      <!-- <el-form-item label="密码" prop="password">
        <el-input
          v-model.trim="form.password"
          type="password"
          autocomplete="off"
        ></el-input>
      </el-form-item> -->

      <!-- <el-form-item label="权限" prop="permissions">
        <el-checkbox-group v-model="form.permissions">
          <el-checkbox label="admin"></el-checkbox>
          <el-checkbox label="editor"></el-checkbox>
        </el-checkbox-group>
      </el-form-item> -->
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="close">
        {{ $t('common.cancel') /*取 消*/ }}
      </el-button>
      <el-button type="primary" @click="save">
        {{ $t('common.sure') /*确 定*/ }}
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
  import { doEdit } from '@/api/userManagement'
  import { getCanAgentList, addAgentUser } from '@/api/agents.js'
  import { setUserInfo } from '@/api/user.js'
  export default {
    name: 'UserManagementEdit',
    data() {
      return {
        form: {
          username: '',
          password: '',
          email: '',
          permissions: [],
        },
        rules: {
          username: [
            { required: true, trigger: 'blur', message: '请输入用户名' },
          ],
          password: [
            { required: true, trigger: 'blur', message: '请输入密码' },
          ],
          email: [{ required: true, trigger: 'blur', message: '请输入邮箱' }],
          permissions: [
            { required: true, trigger: 'blur', message: '请选择权限' },
          ],
        },
        title: '',
        dialogFormVisible: false,
        canAddAgentList: [],
      }
    },
    created() {},
    methods: {
      getCanAddAgentList() {
        getCanAgentList().then((res) => {
          if (res.code == 0) {
            this.canAddAgentList = res.data
          }
        })
      },
      showEdit(row) {
        if (!row) {
          this.title = '添加'
        } else {
          this.title = '编辑'
          this.form = Object.assign({}, row)
        }
        this.getCanAddAgentList()
        this.dialogFormVisible = true
      },
      close() {
        this.$refs['form'].resetFields()
        this.form = this.$options.data().form
        this.dialogFormVisible = false
      },
      save() {
        this.$refs['form'].validate(async (valid) => {
          if (valid) {
            let bmsg = ''
            if (this.form.user_id) {
              const { msg } = await setUserInfo(this.form)
              bmsg = msg
            } else {
              const { msg } = await addAgentUser(this.form)
              bmsg = msg
            }

            this.$baseMessage(bmsg, 'success')
            this.$emit('fetch-data')
            this.close()
          } else {
            return false
          }
        })
      },
    },
  }
</script>
